
import os
import sys

from PySide2.QtWidgets import QApplication
from PySide2.QtGui import QDesktopServices
from PySide2.QtNetwork import QHostAddress, QSslSocket
from PySide2.QtCore import (QObject, QCoreApplication,QFile, QFileInfo, QUrl,QThread)
from PySide2.QtWebChannel import QWebChannel
from PySide2.QtWebSockets import QWebSocketServer

from websocketclientwrapper import WebSocketClientWrapper
class KeyBrdRdr(QThread):
    def __init__(self, parent=None):
        super().__init__(parent)
        pass
    def run(self):
        while (True):
            c = sys.stdin.read(1)
            print(c)
            if(c=='q'):
                print("is q=========")
                break
if __name__ == '__main__':
    app = QCoreApplication(sys.argv)
    if not QSslSocket.supportsSsl():
        print('The example requires SSL support.')
        sys.exit(-1)
    # cur_dir = os.path.dirname(os.path.abspath(__file__))
    # js_file_info = QFileInfo(f"{cur_dir}/qwebchannel.js")
    # if not js_file_info.exists():
    #     QFile.copy(":/qtwebchannel/qwebchannel.js",
    #                js_file_info.absoluteFilePath())

    # setup the QWebSocketServer
    server = QWebSocketServer("QWebChannel Standalone Example Server",
                              QWebSocketServer.NonSecureMode)
    if not server.listen(QHostAddress.LocalHost, 8080):
        print("Failed to open web socket server.")
        sys.exit(-1)
    else:
        print("runing...")

    client_wrapper = WebSocketClientWrapper(server)
    k=KeyBrdRdr()
    k.start()
    k.finished.connect(app.quit)

    sys.exit(app.exec_())
